<template>
	<view>
		<!--  -->
		<!-- :height="90" -->
		<view class="ml32 mr32">
			<view class="flex-end">
				<view class="" style="width: 150rpx;"><u-button shape="circle" type="success" size="mini" text="发送"
						@click="addFriendGroup"></u-button></view>
			</view>
			<u--textarea v-model="activityVo.utitle" :maxlength="-1" :showConfirmBar="false" :border="none"
				:height="200" :autoHeight="true" :focus="true" count placeholder="请输入内容"></u--textarea>
			<u-gap height="20" bgColor="#fff"></u-gap>
			<!--  -->
			<view class="flex-align">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="10"></u-upload>
				<u-icon name="map-fill" size="30" @click="getAddress1"></u-icon>
			</view>
		</view>

		<!--  -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				utitle: '',
				fileList1: [],
				activityVo: {
					annexList: [],
					// udetail: '',
					// ufileType: '',
					/* w */
					ulatitude: 0,
					/* j */
					ulongitude: 0,
					utitle: '',
					uaddress: ''
				}
			};
		},
		methods: {
			// 定位授权
			getLocation() {
				let that = this;
				// 1、判断手机定位服务【GPS】 是否授权
				uni.getSystemInfo({
					success(res) {
						console.log('判断手机定位服务是否授权:', res);
						let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
						let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
						if (locationEnabled == false || locationAuthorized == false) {
							//手机定位服务（GPS）未授权
							uni.showToast({
								title: '请打开手机GPS',
								icon: 'none'
							});
						} else {
							//手机定位服务（GPS）已授权
							// 2、判断微信小程序是否授权位置信息
							// 微信小程序已授权位置信息
							uni.authorize({
								//授权请求窗口
								scope: 'scope.userLocation', //授权的类型
								success: res => {
									that.fnGetlocation();
								},
								fail: err => {
									err = err['errMsg'];
									uni.showModal({
										content: '需要授权位置信息',
										confirmText: '确认授权'
									}).then(res => {
										console.log(res);
										if (res[1]['confirm']) {
											uni.openSetting({
												success: res => {
													if (res.authSetting[
															'scope.userLocation'
															]) {
														// 授权成功
														uni.showToast({
															title: '授权成功',
															icon: 'none'
														});
														that.fnGetlocation();
													} else {
														// 未授权
														uni.showToast({
															title: '授权失败,请重新授权',
															icon: 'none'
														});
														uni.showModal({
															title: '授权',
															content: '获取授权' +
																authouName +
																'失败,是否前往授权设置？',
															success: function(
																result
																) {
																if (result
																	.confirm
																	) {
																	uni
																.openSetting();
																}
															},
															fail: function() {
																uni.showToast({
																	title: '系统错误！',
																	icon: 'none'
																});
															}
														});
													}
												}
											});
										}
										if (res[1]['cancel']) {
											// 取消授权
											uni.showToast({
												title: '你拒绝了授权，无法获得周边信息',
												icon: 'none'
											});
										}
									});
								},
								complete(res) {
									// console.log('授权弹框', res);
									if (res.errMsg == 'authorize:ok') {
										that.fnGetlocation();
									} else {
										uni.showModal({
											title: '授权',
											content: '获取授权' + authouName + '失败,是否前往授权设置？',
											success: function(result) {
												if (result.confirm) {
													uni.openSetting();
												}
											},
											fail: function() {
												uni.showToast({
													title: '系统错误！',
													icon: 'none'
												});
											}
										});
									}
								}
							});
						}
					}
				});
			},

			getAddress1(e) {
				let that = this;
				console.log(111, 'locations');
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						that.activityVo.uaddress = res.name;
						that.activityVo.ulatitude = res.latitude;
						that.activityVo.ulongitude = res.longitude;
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
					}
				});
			},
			async addFriendGroup() {
				/*
				 ufileType
				 1 img
				 2 video
				 3 doc txt
				 4 rar
				 */
				try {
					let re = await this.$API.trendsApi.addTrends(this.activityVo);
					uni.showToast({
						title: '创建成功'
					});
				} catch (e) {
					//TODO handle the exception
					uni.showToast({
						title: e.msg
					});
				}
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				console.log(this.fileList1, 'fileList1');
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map(item => {
					console.log(this.fileList1, 'fileList1');
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					});
				});
				for (let i = 0; i < lists.length; i++) {
					console.log(this.fileList1, 'fileList1');
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: 'success',
							message: '',
							url: result
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let aa = uni.getStorageSync('token');
					let a = uni.uploadFile({
						// url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						// url: 'http://192.168.3.5:8080/file/upload', // 仅为示例，非真实的接口地址
						url: 'https://api.imquanzi.com/prod-api/file/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							Authorization: `Bearer ${aa}`
						},
						success: res => {
							setTimeout(() => {
								let {
									data
								} = JSON.parse(res.data);
								console.log(data, 'data');
								this.activityVo.annexList.push(data.url);
								resolve(res.data.data);
							}, 1000);
						}
					});
				});
			}
		}
	};
</script>

<style lang="scss"></style>